package com.api.user.service.impl;

import com.api.user.dao.PointsTransactionMapper;
import com.api.user.dao.UserInfoMapper;
import com.api.user.model.PointsTransaction;
import com.api.user.model.UserInfo;
import com.api.user.service.PointsTransactionService;
import com.api.core.service.AbstractService;
import com.api.user.vo.PointsTransactionVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/03/26.
 */
@Service
@Transactional
public class PointsTransactionServiceImpl extends AbstractService<PointsTransaction> implements PointsTransactionService {
    @Resource
    private PointsTransactionMapper pointsTransactionMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Override
    public Result list(String search, String order, Integer page, Integer size){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> res = pointsTransactionMapper.baseList(params, orderParams);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result addOrSub(PointsTransaction pointsTransaction) {
        if(pointsTransaction.getFromUserId()!=null){
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(pointsTransaction.getFromUserId());
            pointsTransaction.setPointBefore(userInfo.getPoint());
            userInfo.setPoint(userInfo.getPoint().subtract(pointsTransaction.getPointAmount()));
            pointsTransaction.setPointAfter(userInfo.getPoint());
            userInfoMapper.updateByPrimaryKeySelective(userInfo);
        }

        if(pointsTransaction.getToUserId()!=null){
            UserInfo toUser = userInfoMapper.selectByPrimaryKey(pointsTransaction.getToUserId());
            pointsTransaction.setToPointBefore(toUser.getPoint());
            toUser.setPoint(toUser.getPoint().add(pointsTransaction.getPointAmount()));
            pointsTransaction.setToPointAfter(toUser.getPoint());
            userInfoMapper.updateByPrimaryKeySelective(toUser);
        }
        save(pointsTransaction);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result listUser(Map<String, Object> params, String order, Integer page, Integer size) {
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
            if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
            if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
        }
        PageHelper.startPage(page, size);
        List<PointsTransactionVo> pointsTransactions =  pointsTransactionMapper.list(params,orderParams);
        PageInfo<PointsTransactionVo> pageInfo = new PageInfo<>(pointsTransactions);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result giveUser(Long toUser, BigDecimal point, Long createId) {
        PointsTransaction pointsTransaction = new PointsTransaction();
        pointsTransaction.setToUserId(toUser);
        pointsTransaction.setRemark("give");
        pointsTransaction.setTransactionTime(new Date().getTime());
        pointsTransaction.setTransactionType(8);
        pointsTransaction.setPointAmount(point);
        pointsTransaction.setOrderId(0L);
        pointsTransaction.setCreateId(createId);
        addOrSub(pointsTransaction);
        return ResultGenerator.genSuccessResult();
    }
}
